<?php

namespace app\platform\model;

use app\common\model\CommonBase;
use Exception;
use think\Collection;

class RoleMenuRelation extends CommonBase
{
    public function getAllRoleMenuRelation(array $where = []): Collection
    {
        $where['is_delete'] = config('status.mysql.table_normal');
        return $this->where($where)->select();
    }

    /**
     * @throws Exception
     */
    public function updateRoleMenuRelation(int $roleId, array $rows): bool
    {
        try {
            $this->startTrans();
            $this->where(['role_id' => $roleId])->update([
                'is_delete'  => config('status.mysql.table_delete'),
                'created_at' => date('Y-m-d H:i:s'),
            ]);

            foreach ($rows as $row) {
                $row['status']     = self::IS_ENABLE_STATUS;
                $row['is_delete']  = config('status.mysql.table_normal');
                $row['updated_at'] = date('Y-m-d H:i:s');
                $row['created_at'] = date('Y-m-d H:i:s');
                $this->insert($row);
            }

            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollback();
            throw new Exception(['msg' => $e->getMessage()]);
        }
    }

}
